package com.smartleanx.module.hrm.service.welcomemessage;

import com.smartleanx.framework.common.enums.CommonStatusEnum;
import com.smartleanx.framework.security.core.util.SecurityFrameworkUtils;
import com.smartleanx.module.hrm.intergration.AdminUserServiceProvider;
import com.smartleanx.module.system.api.user.dto.AdminUserRespDTO;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.smartleanx.module.hrm.controller.admin.welcomemessage.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.welcomemessage.WelcomeMessageDO;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.welcomemessage.WelcomeMessageMapper;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 欢迎语 Service 实现类
 *
 * @author 精益大师
 */
@Service
@Validated
public class WelcomeMessageServiceImpl implements WelcomeMessageService {

    @Resource
    private WelcomeMessageMapper welcomeMessageMapper;

    @Resource
    AdminUserServiceProvider adminUserServiceProvider;

    @Override
    public Long createWelcomeMessage(WelcomeMessageSaveReqVO createReqVO) {
        // 插入
        WelcomeMessageDO welcomeMessage = BeanUtils.toBean(createReqVO, WelcomeMessageDO.class);
        welcomeMessageMapper.insert(welcomeMessage);
        // 返回
        return welcomeMessage.getId();
    }

    @Override
    public void updateWelcomeMessage(WelcomeMessageSaveReqVO updateReqVO) {
        // 校验存在
        validateWelcomeMessageExists(updateReqVO.getId());
        // 更新
        WelcomeMessageDO updateObj = BeanUtils.toBean(updateReqVO, WelcomeMessageDO.class);
        welcomeMessageMapper.updateById(updateObj);
    }

    @Override
    public void deleteWelcomeMessage(Long id) {
        // 校验存在
        validateWelcomeMessageExists(id);
        // 删除
        welcomeMessageMapper.deleteById(id);
    }

    private void validateWelcomeMessageExists(Long id) {
        if (welcomeMessageMapper.selectById(id) == null) {
            throw exception(WELCOME_MESSAGE_NOT_EXISTS);
        }
    }

    @Override
    public WelcomeMessageDO getWelcomeMessage(Long id) {
        return welcomeMessageMapper.selectById(id);
    }

    @Override
    public PageResult<WelcomeMessageDO> getWelcomeMessagePage(WelcomeMessagePageReqVO pageReqVO) {
        return welcomeMessageMapper.selectPage(pageReqVO);
    }

    @Override
    public WelcomeMessageDO getWelcomeMessage() {
        WelcomeMessageDO welcomeMessage = welcomeMessageMapper.getCurrentWelcomeMessage();
        if (welcomeMessage != null) {
            return welcomeMessage;
        }
        // 初始化
        welcomeMessage = new WelcomeMessageDO();
        welcomeMessage.setId(null);
        welcomeMessage.setContent("");
        welcomeMessage.setStatus(CommonStatusEnum.DISABLE.getStatus());

        return welcomeMessage;
    }

    @Override
    public Long saveWelcomeMessage(WelcomeMessageSaveReqVO saveReqVO) {
        WelcomeMessageDO welcomeMessage = welcomeMessageMapper.getCurrentWelcomeMessage();
        if (welcomeMessage == null) {
            // 初始化
            welcomeMessage = new WelcomeMessageDO();
        }
        welcomeMessage.setContent(saveReqVO.getContent());
        welcomeMessage.setStatus(saveReqVO.getStatus());



        return welcomeMessage.getId();
    }

    @Override
    public Boolean showWelcomeMessage() {
        AdminUserRespDTO adminUser = adminUserServiceProvider.getAdminUser(SecurityFrameworkUtils.getLoginUserId());
        return adminUser.getLoginDate() == null;
    }
}